//////////////////////////////////////////////////////////////////////////////////
// Project: Falling Blocks (Tetris)
// File:    Main.cpp
//////////////////////////////////////////////////////////////////////////////////

// These three lines link in the required SDL components for our project. //
// Alternatively, we could have linked them in our project settings.      //
#pragma comment(lib, "SDL.lib")
#pragma comment(lib, "SDLmain.lib")
#pragma comment(lib, "SDL_TTF.lib")
#pragma comment(lib, "SDL_image.lib")


#include <stack>     // We'll use the STL stack to store our function pointers
#include <vector>    // An STL vector will store the squares that are not part of the focus block
#include "time.h"    // We use time(), located in "time.h", to seed our random generator
#include "math.h"    // We'll be using the abs() function located in "math.h"
#include "SDL.h"     // Main SDL header 
#include "SDL_TTF.h" // True Type Font header
#include "SDL_image.h"
#include "Defines.h" // Our defines header
#include "Enums.h"   // Our enums header
 
#include "cPlayer.h"
#include "cNonPlayer.h"
#include "cDropObject.h"
using namespace std;   

// The STL stack can't take a function pointer as a type //
// so we encapsulate a function pointer within a struct. //
struct StateStruct 
{
	void (*StatePointer)();
};

// Global data //
stack<StateStruct> g_StateStack;        // Our state stack
SDL_Surface*       g_Bitmap = NULL;     // Our back and squares bitmap
SDL_Surface*       g_Window = NULL;     // Our backbuffer
SDL_Surface*       g_Background = NULL; 
SDL_Surface*       g_Ro = NULL;

SDL_Surface*       g_Trung = NULL;
SDL_Surface*       g_Vit = NULL;
SDL_Surface*       g_Ga = NULL;
SDL_Surface*       g_Ngan = NULL;
SDL_Surface*       g_Shit = NULL;
SDL_Surface*		g_Broken_Egg = NULL;
///////////


SDL_Event		   g_Event;   // An SDL event structure for input
int				   g_Timer;// Our timer is just an integer


//Player/////////////////
cPlayer*		g_Player = NULL;

/////////////////////////
vector<cNonPlayer*> g_NonPlayerList;


vector<cDropObject*> g_DropObjectList;
////////////////////////////
 
int				   g_Score = 0;         // Players current score
int				   g_Level = 0;         // Current level player is on
int				g_ShitCount = 0;
int				g_EggCount = 0;
int				g_BrokenEggCount = 0;
// Init and Shutdown functions //
void Init();
void Shutdown();

// Functions to handle the states of the game //
void Menu();
void Game();
void Exit();

void GameLost();

void AddDrop(int _X, int r, int lev);

// Helper functions for the main game state functions //
void DrawBackground();
void ClearScreen();
void DisplayText(string text, int x, int y, int size, int fR, int fG, int fB, int bR, int bG, int bB);
void HandleMenuInput();
void HandleGameInput();
void HandleExitInput();
void HandleWinLoseInput();

bool CheckPlayer_WallCollisions(cPlayer* player, Direction dir);

bool CheckCollison_Player_DropObject(cDropObject* dobj, cPlayer* pl );


void CheckLoss();

bool CheckSpeedChange(int s);

int main(int argc, char **argv)
{
	Init();
	
	// Our game loop is just a while loop that breaks when our state stack is empty //
	while (!g_StateStack.empty())
	{
		g_StateStack.top().StatePointer();		
	}

	Shutdown();

	return 0;
}


// This function initializes our game //
void Init()
{
	// Initiliaze SDL video and our timer //
	SDL_Init( SDL_INIT_EVERYTHING);
	// Setup our window's dimensions, bits-per-pixel (0 tells SDL to choose for us), //
	// and video format (SDL_ANYFORMAT leaves the decision to SDL). This function    //
	// returns a pointer to our window which we assign to g_Window.                  //
	g_Window = SDL_SetVideoMode(WINDOW_WIDTH, WINDOW_HEIGHT, 0, SDL_ANYFORMAT);    
	// Set the title of our window //
	SDL_WM_SetCaption(WINDOW_CAPTION, 0);
	// Get the number of ticks since SDL was initialized //
	g_Timer = SDL_GetTicks();

	// Fill our surface with information //
	
	//background
	
	SDL_Surface* Surf_Temp = NULL;
	Surf_Temp = IMG_Load("Data/background.png");
	g_Background = SDL_DisplayFormatAlpha(Surf_Temp);

	Surf_Temp = NULL;
	Surf_Temp = IMG_Load("Data/ga.png");
	g_Ga = SDL_DisplayFormatAlpha(Surf_Temp);

	Surf_Temp = NULL;
	Surf_Temp = IMG_Load("Data/vit.png");
	g_Vit = SDL_DisplayFormatAlpha(Surf_Temp);

	Surf_Temp = NULL;
	Surf_Temp = IMG_Load("Data/ngan.png");
	g_Ngan = SDL_DisplayFormatAlpha(Surf_Temp);

	Surf_Temp = NULL;
	Surf_Temp = IMG_Load("Data/egg.png");
	g_Trung = SDL_DisplayFormatAlpha(Surf_Temp);

	Surf_Temp = NULL;
	Surf_Temp = IMG_Load("Data/shit.png");
	g_Shit = SDL_DisplayFormatAlpha(Surf_Temp);

	Surf_Temp = NULL;
	Surf_Temp = IMG_Load("Data/broken_egg.png");
	g_Broken_Egg = SDL_DisplayFormatAlpha(Surf_Temp);

	SDL_FreeSurface(Surf_Temp);


	// Seed our random number generator //
	srand( time(0) );

	//Initealize player//
	g_Player = new cPlayer(PLAYER_START_X, PLAYER_START_Y, g_Ro,PLAYER_WIDTH, PLAYER_HEIGHT);
	
	g_Player->OnLoad("Data/Ro.png");

	// We start by adding a pointer to our exit state, this way //
	// it will be the last thing the player sees of the game.   //
	StateStruct state;
	state.StatePointer = Exit;
	g_StateStack.push(state);

	// Then we add a pointer to our menu state, this will //
	// be the first thing the player sees of our game.    //
	state.StatePointer = Menu;
	g_StateStack.push(state);

	// Initialize the true type font library //
	TTF_Init();
}

// This function shuts down our game //
void Shutdown()
{
	// Shutdown the true type font library //
	TTF_Quit();

	// Free our surfaces //
	SDL_FreeSurface(g_Ro);
	SDL_FreeSurface(g_Background);
	SDL_FreeSurface(g_Window);
	SDL_FreeSurface(g_Ga);
	SDL_FreeSurface(g_Vit);
	SDL_FreeSurface(g_Ngan);
	SDL_FreeSurface(g_Trung);
	SDL_FreeSurface(g_Shit);
	SDL_FreeSurface(g_Broken_Egg);


	// Delete//
	delete g_Player;

	for (int i = 0; i<g_NonPlayerList.size(); i++)
	{
		delete g_NonPlayerList[i];
	}

	for (int j = 0; j<g_DropObjectList.size();j++)
	{
		delete g_DropObjectList[j];
	}


	// Tell SDL to shutdown and free any resources it was using //
	SDL_Quit();
}

// This function handles the game's main menu. From here //
// the player can select to enter the game, or quit.     //
void Menu()
{
	// Here we compare the difference between the current time and the last time we //
	// handled a frame. If FRAME_RATE amount of time has passed, it's time for a new frame. //
	if ( (SDL_GetTicks() - g_Timer) >= FRAME_RATE )
	{
		// We start by calling our input function //
		HandleMenuInput();

		// Make sure nothing from the last frame is still drawn //
		ClearScreen();

		DisplayText("Press [G] To Start (G)ame", 230, 150, 30, 255, 255, 255, 0, 0, 0);
		DisplayText("Press [ESC] or [Q] To (Q)uit Game",  180, 230, 30, 255, 255, 255, 0, 0, 0);
			
		// Tell SDL to display our backbuffer. The four 0's will make //
		// SDL display the whole screen. //
		SDL_UpdateRect(g_Window, 0, 0, 0, 0);

		// We've processed a frame so we now need to record the time at which we did it. //
		// This way we can compare this time with the next time our function gets called //
		// and see if enough time has passed. //
		g_Timer = SDL_GetTicks();
	}	
}

// This function handles the main game. We'll control the   //
// drawing of the game as well as any necessary game logic. //
void Game()
{	
	// Here we compare the difference between the current time and the last time we //
	// handled a frame. If FRAME_RATE amount of time has, it's time for a new frame. //
	if ( (SDL_GetTicks() - g_Timer) >= FRAME_RATE )
	{
		ClearScreen();
		// Draw the background //
		DrawBackground();
		
		for(int i = 0; i < g_NonPlayerList.size(); i++)
		{
			g_NonPlayerList[i]->Draw(g_Window,g_NonPlayerList[i]->Get_X());
			g_NonPlayerList[i]->Move();
			int time_temp = SDL_GetTicks() - g_NonPlayerList[i]->GetDelta();
			if(time_temp  > DROPTIME_LIMIT)
			{
				g_NonPlayerList[i]->SetDelta(SDL_GetTicks());
				AddDrop(g_NonPlayerList[i]->Get_X(),(rand()%7),g_Level);
			}
			
		}
		
		for(int j = 0; j< g_DropObjectList.size(); j ++)
		{
			g_DropObjectList[j]->MoveDown();
			
			if(CheckCollison_Player_DropObject(g_DropObjectList[j],g_Player) && (g_DropObjectList[j]->GetCatchState() == false) &&
				(g_DropObjectList[j]->GetLifeState() == true))
			{
				if(g_DropObjectList[j]->Get_DropType() == DROP_EGG)
				{
					g_EggCount ++;
					if(CheckSpeedChange(g_EggCount))
					{
						g_Level++;
						for (int f = 0; f<g_DropObjectList.size();f++)
						{
							g_DropObjectList[f]->SetDropSpeed_Y(
							g_DropObjectList[f]->GetDropSpeed_Y() + SPEED_CHANGE);
						}
					}

					bool state = true;
					g_DropObjectList[j]->SetCatchState(true);
					bool life = false;
					g_DropObjectList[j]->SetLifeState(life);
				}
				if(g_DropObjectList[j]->Get_DropType() == DROP_SHIT)
				{
					g_ShitCount++;
					///
					CheckLoss();
					////
					bool life = false;
					g_DropObjectList[j]->SetLifeState(life);
				}
				
			}
			if(g_DropObjectList[j]->GetDrop_Y() <= PLAYER_START_Y + 5)
			{
				g_DropObjectList[j]->Draw(g_Window,g_DropObjectList[j]->GetDrop_X(),g_DropObjectList[j]->GetDrop_Y());
		
			}
			if(g_DropObjectList[j]->GetDrop_Y() >= PLAYER_START_Y + 10)
			{
				bool life = false;
				g_DropObjectList[j]->SetLifeState(life);
			}
			if((g_DropObjectList[j]->GetLifeState() == false) && 
				g_DropObjectList[j]->GetCatchState() == false &&
				(g_DropObjectList[j]->Get_DropType() == DROP_EGG)&&
				(g_DropObjectList[j]->GetCountState()== false)
				)
			{

				g_BrokenEggCount++;
				///
				CheckLoss();
				////
				if(CheckSpeedChange(g_EggCount + g_BrokenEggCount))
					{
						g_Level++;
						for (int f = 0; f<g_DropObjectList.size();f++)
						{
							g_DropObjectList[f]->SetDropSpeed_Y(
							g_DropObjectList[f]->GetDropSpeed_Y() + SPEED_CHANGE);
						}
					}
				bool c = true;
				g_DropObjectList[j]->SetCountState(c);
			}
		}
		HandleGameInput();
		/////////////////////
		char temp[256]; 

		string _egg = "x:";
		itoa(g_EggCount, temp, 10);
		_egg.append( temp );

		string _shitcount = "x:";
		itoa(g_ShitCount, temp, 10);
		_shitcount.append(temp);

		string _broken = "x:";
		itoa(g_BrokenEggCount, temp, 10);
		_broken.append(temp);
			
		DisplayText(_egg, EGG_RECT_X, EGG_RECT_Y, 30,255, 255, 255 , 0, 0, 0);
		DisplayText(_shitcount, SHIT_RECT_X, SHIT_RECT_Y, 30,255, 255, 255 , 0, 0, 0);
		DisplayText(_broken, BROKEN_RECT_X, BROKEN_RECT_Y, 30, 255, 255, 255 , 0, 0, 0);
		
		
		/////dram player

		g_Player->Draw(g_Window, g_Player->GetPlayer_X(),g_Player->GetPlayer_Y());
		// Tell SDL to display our backbuffer. The four 0's will make //
		// SDL display the whole screen. //
		SDL_UpdateRect(g_Window, 0, 0, 0, 0);

		// We've processed a frame so we now need to record the time at which we did it. //
		// This way we can compare this time the next time our function gets called and  //
		// see if enough time has passed between iterations. //
		g_Timer = SDL_GetTicks();
	}	
}


void AddDrop(int _X, int ran, int lev)
{
	cDropObject* temp_ = NULL;
	bool state = false;
	bool life = true;
	switch(ran)
	{
	case 0:
	case 2:
	case 3:
	case 4:
	case 5:
	case 6:
		{
			temp_ = new cDropObject(_X,DROP_OBJECT_START_Y,DROP_EGG,EGG_WIDTH, EGG_HEIGHT,state, life,lev);
			temp_->OnLoad(g_Trung);		
			
		}break;
	case 1:
		{
			temp_ = new cDropObject(_X,DROP_OBJECT_START_Y,DROP_SHIT, SHIT_WIDTH, SHIT_HEIGHT,state, life, lev);
			temp_->OnLoad(g_Shit);	
		}break;
	}

	g_DropObjectList.push_back(temp_);
}

bool CheckCollison_Player_DropObject(cDropObject* dobj, cPlayer* pl )
{
	int left1	, left2;
	int right1	, right2;
	int top1	, top2;
	int bottom1	, bottom2;

	left1 = dobj->GetDrop_X();
	left2 = pl->GetPlayer_X();

	right1 = left1 + dobj->GetWidth() - 2;
	right2 = left2 + pl->GetWidth() - 1;

	top1 = dobj->GetDrop_Y();
	top2 = pl->GetPlayer_Y() + 40;

	bottom1 = top1 + dobj->GetHeight() - 1;
	bottom2 = pl->GetPlayer_Y() + pl->GetHeight() - 1;


	
	if(bottom1 < top2) return false;
	if(top1> bottom2) return false;
	if(right1 < left2) return false;
	if(left1 > right2) return false;


	return true;
}
// This function handles the game's exit screen. It will display //
// a message asking if the player really wants to quit.          //
void Exit()
{	
	// Here we compare the difference between the current time and the last time we //
	// handled a frame. If FRAME_RATE amount of time has, it's time for a new frame. //
	if ( (SDL_GetTicks() - g_Timer) >= FRAME_RATE )
	{
		HandleExitInput();

		// Make sure nothing from the last frame is still drawn. //
		ClearScreen();

		DisplayText("Quit Game (Y or N)?", 180, 150, 30, 255, 255, 255, 0, 0, 0);

		// Tell SDL to display our backbuffer. The four 0's will make //
		// SDL display the whole screen. //
		SDL_UpdateRect(g_Window, 0, 0, 0, 0);

		// We've processed a frame so we now need to record the time at which we did it. //
		// This way we can compare this time the next time our function gets called and  //
		// see if enough time has passed between iterations. //
		g_Timer = SDL_GetTicks();
	}	
}


// Display a game over message. //
void GameLost()
{	
	if ( (SDL_GetTicks() - g_Timer) >= FRAME_RATE )
	{
		HandleWinLoseInput();

		ClearScreen();

		DisplayText("You Lose!!!!", 250, 120, 50, 255, 255, 255, 0, 0, 0);
		DisplayText("Quit Game ??:", 120, 210, 30, 255, 255, 255, 0, 0, 0);
		DisplayText("Press [Y] to Quit Game.", 250, 250, 25, 255, 255, 255, 0, 0, 0);
		DisplayText("Press [N] to Continute.", 250, 300, 25, 255, 255, 255, 0, 0, 0);

		SDL_UpdateRect(g_Window, 0, 0, 0, 0);

		g_Timer = SDL_GetTicks();
	}	
}

// This function draws the background //
void DrawBackground() 
{
	SDL_Rect source ;
	SDL_Rect temp = { 0, 0, WINDOW_WIDTH, WINDOW_HEIGHT };

	source = temp;
	SDL_Rect destination = { 0, 0, WINDOW_WIDTH, WINDOW_HEIGHT };
	
	SDL_BlitSurface(g_Background, &source, g_Window, &destination);
	
	//egg

	SDL_Rect egg_img ;
	SDL_Rect temp_e = {0,0,EGG_WIDTH,EGG_HEIGHT};
	egg_img = temp_e;

	SDL_Rect destination_e = { IMG_EGG_X, IMG_EGG_Y, EGG_WIDTH, EGG_HEIGHT };
	SDL_BlitSurface(g_Trung, &egg_img, g_Window, &destination_e);
	
	//Shit
	SDL_Rect shit_img ;
	SDL_Rect temp_s = {0,0,SHIT_WIDTH,SHIT_HEIGHT};
	shit_img = temp_e;

	SDL_Rect destination_s = { IMG_SHIT_X, IMG_SHIT_Y, SHIT_WIDTH, SHIT_HEIGHT };
	SDL_BlitSurface(g_Shit, &shit_img, g_Window, &destination_s);
	
	///Broken Egg
	SDL_Rect br_img ;
	SDL_Rect temp_b = {0,0,53,50};
	br_img = temp_b;

	SDL_Rect destination_b = { IMG_BROKEN_X, IMG_BROKEN_Y, 53, 50 };
	SDL_BlitSurface(g_Broken_Egg, &br_img, g_Window, &destination_b);

}

// This function simply clears the back buffer to black //
void ClearScreen()
{
	// This function just fills a surface with a given color. The //
	// first 0 tells SDL to fill the whole surface. The second 0  //
	// is for black. //
	SDL_FillRect(g_Window, 0, 0);
}

// This function displays text to the screen. It takes the text //
// to be displayed, the location to display it, the size of the //
// text, and the color of the text and background.              //
void DisplayText(string text, int x, int y, int size, int fR, int fG, int fB, int bR, int bG, int bB) 
{
	// Open our font and set its size to the given parameter //
    TTF_Font* font = TTF_OpenFont("arial.ttf", size);

	SDL_Color foreground  = { fR, fG, fB};   // text color 
	SDL_Color background  = { bR, bG, bB };  // color of what's behind the text 

	// This renders our text to a temporary surface. There //
	// are other text functions, but this one looks nicer. //
	SDL_Surface* temp = TTF_RenderText_Shaded(font, text.c_str(), foreground, background);

	// A structure storing the destination of our text. //
	SDL_Rect destination = { x, y, 0, 0 };
	
	// Blit the text surface to our window surface. //
	SDL_BlitSurface(temp, NULL, g_Window, &destination);

	// Always free memory! //
	SDL_FreeSurface(temp);

	// Close the font. //
	TTF_CloseFont(font);
}

// This function receives player input and //
// handles it for the game's menu screen.  //
void HandleMenuInput() 
{
	// Fill our event structure with event information. //
	if ( SDL_PollEvent(&g_Event) )
	{
		// Handle user manually closing game window //
		if (g_Event.type == SDL_QUIT)
		{			
			// While state stack isn't empty, pop //
			while (!g_StateStack.empty())
			{
				g_StateStack.pop();
			}

			return;  // game is over, exit the function
		}

		// Handle keyboard input here //
		if (g_Event.type == SDL_KEYDOWN)
		{
			if (g_Event.key.keysym.sym == SDLK_ESCAPE)
			{
				g_StateStack.pop();
				return;  // this state is done, exit the function 
			}
			// Quit //
			if (g_Event.key.keysym.sym == SDLK_q)
			{
				g_StateStack.pop();
				return;  // game is over, exit the function 
			}
			// Start Game //
			if (g_Event.key.keysym.sym == SDLK_g)
			{
				StateStruct temp;
				temp.StatePointer = Game;
				g_StateStack.push(temp);
				return;  // this state is done, exit the function 
			}
		}
	}
}

// This function receives player input and //
// handles it for the main game state.     //
void HandleGameInput() 
{
	// These variables allow the user to hold the arrow keys down //

	static bool left_pressed  = false;
	static bool right_pressed = false;


	// Fill our event structure with event information. //
	if ( SDL_PollEvent(&g_Event) )
	{
		//Handle mouse input
		int x, y;
		SDL_GetMouseState(&x, &y);
		if(g_Event.type == SDL_MOUSEMOTION)
		{
			if(	x < PLAYER_WIDTH/2 + WINDOW_LIMIT_LEFT)
				g_Player->SetPlayer_x(WINDOW_LIMIT_LEFT);
			else if(	x < WINDOW_WIDTH - PLAYER_WIDTH/2)
				g_Player->SetPlayer_x(x - PLAYER_WIDTH/2);
			
		}

		// Handle user manually closing game window //
		if (g_Event.type == SDL_QUIT)
		{			
			// While state stack isn't empty, pop //
			while (!g_StateStack.empty())
			{
				g_StateStack.pop();
			}

			return;  // game is over, exit the function
		}

		// Handle keyboard input here //
		if (g_Event.type == SDL_KEYDOWN)
		{
			if (g_Event.key.keysym.sym == SDLK_ESCAPE)
			{
				g_StateStack.pop();
				
				return;  // this state is done, exit the function 
			}	
			
			// For the left, right, and down arrow keys, we just set a bool variable //
			if (g_Event.key.keysym.sym == SDLK_LEFT)
			{
				left_pressed = true;				
			}
			if (g_Event.key.keysym.sym == SDLK_RIGHT)
			{
				right_pressed = true;
			}

////////////////////////////////
		}

		// If player lifts key, set bool variable to false //
		if (g_Event.type == SDL_KEYUP)
		{
			if (g_Event.key.keysym.sym == SDLK_LEFT)
			{
				left_pressed = false;

			}
			if (g_Event.key.keysym.sym == SDLK_RIGHT)
			{
				right_pressed = false;
			}
////////////////////////////////////////
			if (g_Event.key.keysym.sym == SDLK_g)
			{
				cNonPlayer* temp = NULL;
				temp = new cNonPlayer(rand()%(1000-WINDOW_LIMIT_LEFT)+ WINDOW_LIMIT_LEFT,NON_PLAYER_Y,ENTITY_GA,SDL_GetTicks());
				temp->OnLoad(g_Ga);

				g_NonPlayerList.push_back(temp);
			}
			if (g_Event.key.keysym.sym == SDLK_v)
			{
				cNonPlayer* temp = NULL;
				temp = new cNonPlayer(rand()%(1000-WINDOW_LIMIT_LEFT)+ WINDOW_LIMIT_LEFT,NON_PLAYER_Y,ENTITY_VIT,SDL_GetTicks());
				temp->OnLoad(g_Vit);

				g_NonPlayerList.push_back(temp);
			}
			if (g_Event.key.keysym.sym == SDLK_n)
			{
				cNonPlayer* temp = NULL;
				temp = new cNonPlayer(rand()%(1000-WINDOW_LIMIT_LEFT)+ WINDOW_LIMIT_LEFT,NON_PLAYER_Y,ENTITY_NGAN,SDL_GetTicks());
				temp->OnLoad(g_Ngan);

				g_NonPlayerList.push_back(temp);
			}
////////////////////////////////
		}
	}

	 //Now we handle the arrow keys, making sure to check for collisions //

	if (left_pressed)
	{
		if ( !CheckPlayer_WallCollisions(g_Player, LEFT) )
		{
			int x = g_Player->GetPlayer_X();
			g_Player->SetPlayer_x(x - PLAYER_SPEED);
		}
	}

	if (right_pressed)
	{
		if ( !CheckPlayer_WallCollisions(g_Player, RIGHT) )
		{
			int x = g_Player->GetPlayer_X();
			g_Player->SetPlayer_x(x + PLAYER_SPEED);
		}
	}
/////////////////////////////////////////////
}
/////////////////////
bool CheckPlayer_WallCollisions(cPlayer* player, Direction dir)
{
	int temp_x; // stores the location of the entity after moving
	
	// Get the location of the entity after it moves //
	switch (dir)
	{
		case LEFT:
		{
			temp_x = player->GetPlayer_X() - PLAYER_SPEED;
		} break;
		case RIGHT:
		{
			// Notice that we have to add the entities width to get its right(->) coordinate //
			temp_x = player->GetPlayer_X() + PLAYER_WIDTH + PLAYER_SPEED;
		} break;
	}

	if (temp_x <= PLAYER_WIDTH/2 + WINDOW_LIMIT_LEFT)
	{
			g_Player->SetPlayer_x(WINDOW_LIMIT_LEFT);
			return true;

	}else if (temp_x >= WINDOW_WIDTH)
	{
		g_Player->SetPlayer_x(WINDOW_WIDTH - PLAYER_WIDTH);
		return true;
	}

	return false;
}


// This function receives player input and //
// handles it for the game's exit screen.  //
void HandleExitInput() 
{
	// Fill our event structure with event information. //
	if ( SDL_PollEvent(&g_Event) )
	{
		// Handle user manually closing game window //
		if (g_Event.type == SDL_QUIT)
		{			
			// While state stack isn't empty, pop //
			while (!g_StateStack.empty())
			{
				g_StateStack.pop();
			}

			return;  // game is over, exit the function
		}

		// Handle keyboard input here //
		if (g_Event.type == SDL_KEYDOWN)
		{
			if (g_Event.key.keysym.sym == SDLK_ESCAPE)
			{
				g_StateStack.pop();
				
				return;  // this state is done, exit the function 
			}
			// Yes //
			if (g_Event.key.keysym.sym == SDLK_y)
			{
				g_StateStack.pop();
				return;  // game is over, exit the function 
			}
			// No //
			if (g_Event.key.keysym.sym == SDLK_n)
			{
				StateStruct temp;
				temp.StatePointer = Menu;
				g_StateStack.push(temp);
				return;  // this state is done, exit the function 
			}
		}
	}
}

// Input handling for win/lose screens. //
void HandleWinLoseInput()
{
	if ( SDL_PollEvent(&g_Event) )
	{
		// Handle user manually closing game window //
		if (g_Event.type == SDL_QUIT)
		{			
			// While state stack isn't empty, pop //
			while (!g_StateStack.empty())
			{
				g_StateStack.pop();
			}

			return;  
		}

		// Handle keyboard input here //
		if (g_Event.type == SDL_KEYDOWN)
		{
			if (g_Event.key.keysym.sym == SDLK_ESCAPE)
			{
				g_StateStack.pop();
				
				return;  
			}
			if (g_Event.key.keysym.sym == SDLK_y)
			{
				g_StateStack.pop();
				return;  
			}
			// If player chooses to continue playing, we pop off    //
			// current state and push exit and menu states back on. //
			if (g_Event.key.keysym.sym == SDLK_n)
			{
				g_StateStack.pop();

				StateStruct temp;
				temp.StatePointer = Exit;
				g_StateStack.push(temp);

				temp.StatePointer = Menu;
				g_StateStack.push(temp);
				return;  
			}
		}
	}
}




// Check to see if player has lost. Handle losing condition if needed. //
void CheckLoss() 
{
	if((g_BrokenEggCount >= 3) || (g_ShitCount >= 3))
	{
		for(int i = 0; i< g_NonPlayerList.size(); i++)
		{
			delete g_NonPlayerList[i];
		}
		g_NonPlayerList.clear();
		for(int i = 0; i< g_DropObjectList.size(); i++)
		{
		 	delete g_DropObjectList[i];
		}

		g_DropObjectList.clear();
		g_EggCount = 0;
		g_ShitCount =0;
		g_Level = 0;
		g_BrokenEggCount = 0;

		// Pop all states //
		while (!g_StateStack.empty())
		{
			g_StateStack.pop();
		}

		// Push the losing state onto the stack //
		StateStruct lose;
		lose.StatePointer = GameLost;

		g_StateStack.push(lose);
	}
}

bool CheckSpeedChange(int s)
{
	if(s % CHANGE_CAP == 0)
		return true;
	return false;
}


